// SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
// SPDX-License-Identifier: Mulan PSL v2
package com.geniusai.aip.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.geniusai.aip.auth.domain.UserDO;
import com.geniusai.aip.auth.mapper.UserMapper;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.geniusai.aip.auth.domain.User;

/**
 * @author longping58607
 * @date 2020/12/10
 */
@Configuration
@Log
@Slf4j
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userdomapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = new User();

        UserDO userDO = userdomapper.selectOne(new QueryWrapper<UserDO>().eq("username", username));

        if (userDO == null) {
            // 设置了这个条件后, 必须输入用户名 user
            throw new UsernameNotFoundException("用户名或密码错误");
        }

        user.setUsername(username);

        return user;
    }
}
